package com.wang.vagus.controller;

import com.wang.vagus.base.Result;
import com.wang.vagus.data.dto.LoginDTO;
import com.wang.vagus.data.dto.RegisterDTO;
import com.wang.vagus.data.vo.LoginVO;
import com.wang.vagus.data.vo.RegisterVO;
import com.wang.vagus.entity.User;
import com.wang.vagus.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(value = "UserController", description = "用户相关")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public Result register(RegisterVO registerVO){
        RegisterDTO registerDTO = new RegisterDTO();
        BeanUtils.copyProperties(registerVO,registerDTO);
        User user = userService.register(registerDTO);
        return Result.success(user);
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result login(LoginVO loginVO){
        LoginDTO loginDTO = new LoginDTO();
        BeanUtils.copyProperties(loginVO,loginDTO);
        String token = userService.login(loginDTO);
        return Result.success(token);
    }

    @GetMapping("/info")
    @ApiOperation(value = "获取用户信息")
    public Result info(@RequestHeader(value = "Authorization", required = true,defaultValue = "Bearer ") String jwtToken){
        String token = jwtToken.substring(7);
        User user = userService.getUerInfoByToken(token);
        return Result.success(user);
    }

}
